package io.renren.modules.oss.controller;

import com.github.pagehelper.PageInfo;
import io.renren.common.exception.RRException;
import io.renren.common.pojo.Query;
import io.renren.common.pojo.R;
import io.renren.common.util.ConfigConstant;
import io.renren.common.util.Constant;
import io.renren.common.util.JsonUtils;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AliyunGroup;
import io.renren.common.validator.group.QcloudGroup;
import io.renren.common.validator.group.QiniuGroup;
import io.renren.modules.common.BaseController;
import io.renren.modules.oss.cloud.CloudStorageConfig;
import io.renren.modules.oss.cloud.OSSFactory;
import io.renren.modules.oss.entity.SysOssEntity;
import io.renren.modules.oss.pojo.FileUrl;
import io.renren.modules.oss.service.SysOssService;
import io.renren.modules.sys.service.SysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;


/**
 * 文件上传
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017-03-25 12:13:26
 */
@RestController
@RequestMapping("sys/oss")
@Api(description = "文件服务接口")
public class SysOssController extends BaseController{
	@Autowired
	private SysOssService sysOssService;

    @Autowired
    private SysConfigService sysConfigService;

    @Autowired
	private OSSFactory ossFactory;

    private final static String KEY = ConfigConstant.CLOUD_STORAGE_CONFIG_KEY;
	
	/**
	 * 列表
	 */
	@RequestMapping(value = "/list",method = RequestMethod.GET)
	@PreAuthorize("hasAuthority('sys:oss:all')")
	@ApiOperation("获取文件列表")
	public R<PageInfo<SysOssEntity>> list(Query query){
		PageInfo<SysOssEntity> pageInfo = sysOssService.queryPage(query);
		return R.ok(pageInfo);
	}

    /**
     * 云存储配置信息
     */
    @RequestMapping(value = "/config",method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('sys:oss:all')")
	@ApiOperation("获取云存储配置信息")
    public R<CloudStorageConfig> config(){
        CloudStorageConfig config = sysConfigService.getConfigObject(KEY, CloudStorageConfig.class);
        return R.ok(config);
    }

	/**
	 * 保存云存储配置信息
	 */
	@RequestMapping(value = "/",method = RequestMethod.POST)
	@PreAuthorize("hasAuthority('sys:oss:all')")
	@ApiOperation("保存云存储配置信息")
	public R saveConfig(@ApiParam("参数对象") @RequestBody CloudStorageConfig config){
		//校验类型
		ValidatorUtils.validateEntity(config);

		if(config.getType() == Constant.CloudService.QINIU.getValue()){
			//校验七牛数据
			ValidatorUtils.validateEntity(config, QiniuGroup.class);
		}else if(config.getType() == Constant.CloudService.ALIYUN.getValue()){
			//校验阿里云数据
			ValidatorUtils.validateEntity(config, AliyunGroup.class);
		}else if(config.getType() == Constant.CloudService.QCLOUD.getValue()){
			//校验腾讯云数据
			ValidatorUtils.validateEntity(config, QcloudGroup.class);
		}
		

        sysConfigService.updateValueByKey(KEY, JsonUtils.toJson(config));

		return R.ok();
	}

	/**
	 * 上传文件
	 */
	@RequestMapping(value = "/upload",method = RequestMethod.POST)
	@PreAuthorize("hasAuthority('sys:oss:all')")
	@ApiOperation("上传文件")
	public R<FileUrl> upload(@ApiParam("文件") @RequestParam("file") MultipartFile file) throws Exception {
		if (file.isEmpty()) {
			throw new RRException("上传文件不能为空");
		}

		//上传文件
		String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
		String url = ossFactory.getCloudStorageService().uploadSuffix(file.getBytes(), suffix);

		//保存文件信息
		SysOssEntity ossEntity = new SysOssEntity();
		ossEntity.setUrl(url);
		ossEntity.setCreateDate(new Date());
		sysOssService.save(ossEntity);

		FileUrl fileUrl = new FileUrl(url);
		return R.ok(fileUrl);
	}

	/**
	 * 删除
	 */
	@RequestMapping(value = "/",method = RequestMethod.DELETE)
	@PreAuthorize("hasAuthority('sys:oss:all')")
	public R delete(@ApiParam("文件ID列表") @RequestBody Long[] ids){
		sysOssService.deleteBatch(ids);

		return R.ok();
	}

}
